﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FUSE.AzureConfig.Tasks
{
    /// <summary>
    /// JSON-based azure configuration specification.
    /// </summary>
    /// <example>
    /// {
    ///     name: "MySite",
    ///     roles: { "WebServer": { type: "Web" }, "WorkerRole": { type: "Worker" } },
    ///     envs: {
    ///         common: {
    ///             profileName: "", // empty == not generated.
    ///             roles: { "WebServer": { instances = 1 }, "WorkerRole": { instances = 1 } },
    ///             configSettings: {
    ///                 "WebServer, WorkerRole" : {
    ///                     "setting key 1": "setting value 1"
    ///                 }
    ///             }
    ///         },
    ///         scratch: {
    ///             parent: common,
    ///             profileName: "Scratch Env",
    ///             configSettings: {
    ///                 "WebServer" : {
    ///                     "key 2": "(web) value 2"
    ///                 },
    ///                 "WorkerRole" : {
    ///                     "key 2": "(worker) value 2"
    ///                 }
    ///             }
    ///         },
    ///         scratch2: {
    ///             parent: scratch,
    ///             profileName: "Scratch (2)",
    ///             configSettings: {
    ///                 "WebServer, WorkerRole" : {
    ///                     "key 3": "setting value 3"
    ///                 }
    ///             }
    ///         },
    ///     }
    /// }
    /// </example>
    public class AzureConfigSpec
    {
        public AzureConfigSpec()
        {
            this.Environments = new Dictionary<string, EnvironmentSpec>();
            this.Roles = new List<RoleSpec>();
        }

        public string Name { get; set; }

        [JsonProperty("envs")]
        public Dictionary<string, EnvironmentSpec> Environments { get; set; }

        public List<RoleSpec> Roles { get; set; }
    }

    public class EnvironmentSpec
    {
        public EnvironmentSpec()
        {
            this.Roles = new Dictionary<string, RoleSpec>();
            this.ConfigSettings = new Dictionary<string, Dictionary<string, string>>();
            this.Certificates = new Dictionary<string, Dictionary<string, CertSpec>>();
        }

        public string Parent { get; set; }

        public string ProfileName { get; set; }

        public Dictionary<string, RoleSpec> Roles { get; set; }

        public Dictionary<string, Dictionary<string, string>> ConfigSettings { get; set; }

        public Dictionary<string, Dictionary<string, CertSpec>> Certificates { get; set; }
    }

    public class RoleSpec
    {
        // Usually specified at top level.
        public string Type { get; set; }

        // Usually specified at instance level.
        public int Instances { get; set; }
    }

    public class CertSpec
    {
        public CertSpec()
        {
            this.ThumbprintAlgorithm = "sha1";
        }

        public string Thumbprint { get; set; }

        public string ThumbprintAlgorithm { get; set; }
    }
}
